<template>
	<view class="container">
		<!-- Product Main Info -->
		<view class="product-main">
			<swiper class="swiper" :indicator-dots="true" indicator-color="rgba(0, 0, 0, .3)"
				indicator-active-color="#000">
				<swiper-item v-for="(item, index) in swiperList" :key="index">
					<image :src="item | avatarUrlFind('1')" mode="aspectFill" class="swiper-image"></image>
				</swiper-item>
			</swiper>
		</view>

		<!-- Price Info -->
		<view class="price-info">
			<view class="price-row" v-if="product.goodsCategory!=='JP'">
				<text class="current-price">¥ {{salesPrice}}</text>
				<text class="original-price">¥{{dashingPrice}}</text>
			</view>
			<view class="price-row" v-if="product.goodsCategory==='JP'">
				<view class="current-price points-price">
					<image src="https://cos.ruilongan.com/XCX/2025/08/29/积分log@2x_20250829135415A003.png"
						class="points-icon" mode="aspectFit"></image>
					<text>{{salesPrice}}</text>
				</view>
				<text class="original-price">市场价¥{{dashingPrice}}</text>
			</view>
			<view class="title">{{product.drugName}}</view>
			<view class="subtitle">{{product.productInfo}}</view>
			<view class="tags" v-if="product.goodsCategory!=='JP'">
				<text class="tag tag-green" v-if="product.isFreeShipping">包邮</text>
				<text class="tag tag-green">积分赠送</text>
				<text class="sales">已售：{{product.salesNum+product.virtualSalesNum}}+</text>
			</view>
			<view class="tags" v-if="product.goodsCategory==='JP'">
				<text class="tag tag-orange">每人限购3次</text>
			</view>
		</view>

		<!-- Product Details -->
		<view class="product-details padding">
			<!-- 富文本内容 -->
			<u-parse :content="product.productDetails"></u-parse>
		</view>

		<!-- Bottom Bar -->
		<view class="bottom-bar">
			<!-- 积分商品显示积分信息 -->
			<view class="points-info" v-if="product.goodsCategory==='JP'">
				<text class="points-text">我的控糖积分：{{usableIntegral}}</text>
			</view>

			<view class="share-btn" @click="handleShare">
				<u-icon name="share-square" color="#000000" size="28"></u-icon>
				<text>分享</text>
			</view>
			<view class="buy-btn" @click="showSpecModal" v-if="product.goodsCategory!=='JP'">立即购买</view>
			<view class="buy-btn exchange-btn" @click="showPointsExchangeModal" v-if="product.goodsCategory==='JP'">立即兑换</view>
		</view>

		<!-- Specification Modal -->
			<specification-modal ref="specModal" :product="product" :sales-price="salesPrice" :dashing-price="dashingPrice"
				@spec-change="handleSpecChange"></specification-modal>

			<points-exchange-modal ref="pointsExchangeModal" :product="product" :sales-price="salesPrice"
				:dashing-price="dashingPrice" @spec-change="handleSpecChange"></points-exchange-modal>
	</view>
</template>

<script>
	import SpecificationModal from '@/components/SpecificationModal.vue'
	import PointsExchangeModal from '@/components/PointsExchangeModal.vue'
	import {
		getInfo
	} from '@/api/shop.js'
	import {
		getUserInfo
	} from '@/api/vipUser.js'
	import {
		mapGetters
	} from "vuex"
	import {
		getToken
	} from '@/utils/auth'
	export default {
		computed: mapGetters(['name', 'userId']),
		components: {
			SpecificationModal,
			PointsExchangeModal
		},
		data() {
			return {
				id: '',
				swiperList: [],
				content: '',
				product: {},
				salesPrice: '0',
				dashingPrice: '0',
				usableIntegral: 0
			}
		},
		onLoad(options) {
			this.id = options.id
			this.getInfo()
			this.getUserInfo()
		},
		methods: {
			showSpecModal() {
				
				if (!getToken()) {
					this.$tab.reLaunch('/pages/login')
					return
				}
				this.$refs.specModal.open()
			},
			showPointsExchangeModal() {
				if (!getToken()) {
					this.$tab.reLaunch('/pages/login')
					return
				}
				this.$refs.pointsExchangeModal.open()
			},
			handleSpecChange(specData) {
				this.salesPrice = specData.salesPrice
				this.dashingPrice = specData.dashingPrice
			},
			handleShare() {
				uni.navigateTo({
					url: '/subPackage_1/pages/goods/good-share'
				});
			},
			getInfo() {
				getInfo(this.id).then(res => {
					console.log('-商品详情-', res)
					this.product = res.data
					this.swiperList = this.product.sliderImage.split('|')
					if (this.product.goodsCategory === 'TC') {
						this.salesPrice = this.product.packagePrice || '0'
						this.dashingPrice = this.product.dashingPrice || '0'
					} else if (this.product.goodsCategory === 'P') {
						// 确保有规格列表且不为空
						if (this.product.speciList && this.product.speciList.length > 0) {
							this.salesPrice = this.product.speciList[0].salesPrice || '0'
							this.dashingPrice = this.product.speciList[0].dashingPrice || '0'
						}
					}
					console.log(this.salesPrice)
					console.log(this.dashingPrice)

				})
			},

			getUserInfo() {
				if (this.userId) {
					getUserInfo(this.userId).then(res => {
						this.usableIntegral = res.data.usableIntegral
					})
				}
			}
		}
	}
</script>

<style lang="scss">
	.container {
		// min-height: 100vh;
		background-color: #fff;
		padding-bottom: 90rpx;
	}


	.product-main {
		width: 100%;
		height: 750rpx;
		margin-bottom: -20px;

		.swiper {
			width: 750rpx;
			height: 750rpx;

			.swiper-image {
				width: 100%;
				height: 100%;
			}
		}
	}

	.price-info {
		position: relative;
		padding: 38rpx;
		background: #fff;
		border-top-right-radius: 24px;
		border-top-left-radius: 24px;
		margin-top: -20px;
		z-index: 1;

		.price-row {
			display: flex;
			align-items: baseline;
			margin-bottom: 20rpx;
		}

		.current-price {
			font-size: 52rpx;
			color: #000;
			// font-weight: bold;

			&.points-price {
				display: flex;
				align-items: center;
				color: #F54414;

				.points-icon {
					width: 32rpx;
					height: 32rpx;
					margin-right: 12rpx;
				}
			}
		}

		.original-price {
			font-size: 36rpx;
			color: #999;
			text-decoration: line-through;
			margin-left: 20rpx;
		}

		.title {
			font-size: 40rpx;
			color: #333;
			font-weight: bold;
			margin-bottom: 10rpx;
		}

		.subtitle {
			font-size: 26rpx;
			color: #666;
			margin-bottom: 20rpx;
		}

		.tags {
			display: flex;
			align-items: center;

			.tag {
				padding: 4rpx 16rpx;
				font-size: 24rpx;
				border-radius: 4rpx;
				margin-right: 20rpx;

				&.tag-green {
					background: #09ab4f;
					color: #fff;
				}

				&.tag-orange {
					background: #FAAC1B;
					color: #fff;
				}
			}

			.sales {
				font-size: 24rpx;
				color: #979797;
				margin-left: auto;
			}
		}
	}

	.bottom-bar {
		position: fixed;
		bottom: 0;
		left: 0;
		right: 0;
		height: 166rpx;
		background-color: #fff;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		// padding: 0 30rpx;
		box-shadow: 0 -2rpx 10rpx rgba(0, 0, 0, 0.05);

		.points-info {
			position: absolute;
			top: 6rpx;
			left: 50%;
			transform: translateX(-50%);

			.points-text {
				font-size: 32rpx;
				color: #FF9321;
				font-weight: 500;
			}
		}

		.share-btn {
			position: absolute;
			left: 40rpx;
			bottom: 40rpx;
			display: flex;
			flex-direction: column;
			align-items: center;
			font-size: 32rpx;
			color: #000000;
		}

		.buy-btn {
			position: absolute;
			right: 16rpx;
			bottom: 40rpx;
			width: 300px;
			height: 80rpx;
			background: #07c160;
			color: #fff;
			display: flex;
			align-items: center;
			justify-content: center;
			border-radius: 14rpx;
			font-size: 40rpx;

			&.exchange-btn {
				background: linear-gradient(135deg, #FFC608 0%, #FC3F06 100%);
			}
		}
	}
</style>